package com.leve.admin.controller;

import com.leve.admin.common.ResultUtil;
import com.leve.admin.domain.ResultBase;
import com.leve.admin.service.ShopService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;

@RestController
@RequestMapping("/shops")
@CrossOrigin(origins = {"*"}, maxAge = 3600L)
@Slf4j
public class ShopController {
    @Resource
    private ShopService shopService;

    @ApiOperation("web端查询订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码，从0开始，默认值0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页规模，默认值10", dataType = "int", paramType = "query")})
    @GetMapping(value = "findorders", produces = APPLICATION_JSON_UTF8_VALUE)
    public ResultBase findOrders(@PageableDefault(size = 10) Pageable pageable, HttpServletRequest httpServletRequest) {
        String statusId = httpServletRequest.getParameter("statusId");
        String name = httpServletRequest.getParameter("name");
        return shopService.findOrders(statusId, name, pageable.getPageNumber(), pageable.getPageSize());
    }

    @ApiOperation("upc转账-admin-web")
    @GetMapping(value = "/upctrx")
    @ResponseBody
    public ResultBase upcTransaction(HttpServletRequest request) {
        return shopService.upcTransaction();
    }


    @ApiOperation("提现中")
    @GetMapping(value = "/remit_ing/{gid}")
    @ResponseBody
    public ResultBase remit_ing(@PathVariable("gid") String gid) {
        return shopService.remit_ing(gid);
    }


    @ApiOperation("获取全部提现记录--web")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码，从0开始，默认值0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页规模，默认值10", dataType = "int", paramType = "query")})
    @GetMapping("getall")
    public ResultBase findRemitRecord(HttpServletRequest request, Pageable pageable) {
        String name = request.getParameter("name");
        String statusId = request.getParameter("statusId");
        return shopService.findRemitRecord(name,statusId, pageable.getPageNumber(), pageable.getPageSize());
    }


    @ApiOperation("提现或者退回--web")
    @GetMapping(value = "remit_return")
    @ResponseBody
    public ResultBase remitReturn(HttpServletRequest request) {
        /**
         * 前端不懂怎么写两个窗口,所以在这里用 types区分提现和退款
         */
        String backAmount = request.getParameter("keyValue");
        String rid = request.getParameter("id");
        String types = request.getParameter("checkList");
        return shopService.remitReturn(types, rid, backAmount);
    }


    @ApiOperation("出售中的列表--web端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码，从0开始，默认值0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页规模，默认值10", dataType = "int", paramType = "query")})
    @GetMapping(value = "sellorders", produces = APPLICATION_JSON_UTF8_VALUE)
    public ResultBase selectSellOrders(@PageableDefault(size = 10) Pageable pageable, HttpServletRequest httpServletRequest) {
        String statusId = httpServletRequest.getParameter("statusId");
        String name = httpServletRequest.getParameter("name");
        return shopService.selectSellOrders(statusId, name, pageable.getPageNumber(), pageable.getPageSize());
    }


    @ApiOperation("购买--web端用")
    @GetMapping(value = "addbuy", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase addBuy(HttpServletRequest request) {
        String buyAmount = request.getParameter("keyValue");
        String sid = request.getParameter("id");
        return shopService.addBuy(buyAmount, sid);
    }

    @ApiOperation("购买列表--web端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码，从0开始，默认值0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页规模，默认值10", dataType = "int", paramType = "query")})
    @GetMapping(value = "buyorders", produces = APPLICATION_JSON_UTF8_VALUE)
    public ResultBase selectBuyOrders(@PageableDefault(size = 10) Pageable pageable, HttpServletRequest httpServletRequest) {
        String statusId = httpServletRequest.getParameter("statusId");
        String name = httpServletRequest.getParameter("name");
        return shopService.selectBuyOrders(statusId, name, pageable.getPageNumber(), pageable.getPageSize());
    }


    @ApiOperation("充值大厅--订单状态-admin-web")
    @GetMapping("getstatus")
    public ResultBase getstatus() {

        Map<String, Object> tstatus1 = new HashMap<>();
        tstatus1.put("keyValue", "TRADE_SUCCESS");
        tstatus1.put("keyName", "完成");

        Map<String, Object> tstatus2 = new HashMap<>();
        tstatus2.put("keyValue", "WAIT_BUYER_PAY");
        tstatus2.put("keyName", "待支付");

        Map<String, Object> tstatus3 = new HashMap<>();
        tstatus3.put("keyValue", "TRADE_CLOSED");
        tstatus3.put("keyName", "交易关闭");

        Map<String, Object> tstatus4 = new HashMap<>();
        tstatus4.put("keyValue", "SYS_CHECK");
        tstatus4.put("keyName", "系统确认中");

        Map<String, Object> tstatus5 = new HashMap<>();
        tstatus5.put("keyValue", "PAST_DUE");
        tstatus5.put("keyName", "订单过期");

        Map<String, Object> tstatus6 = new HashMap<>();
        tstatus6.put("keyValue", "TRADE_REMIT");
        tstatus6.put("keyName", "提现扣款");

        Map<String, Object> tstatus7 = new HashMap<>();
        tstatus6.put("keyValue", "TRADE_RETURN");
        tstatus6.put("keyName", "提现退回");

        List<Map> list = new ArrayList<>();
        list.add(tstatus1);
        list.add(tstatus2);
        list.add(tstatus3);
        list.add(tstatus4);
        list.add(tstatus5);
        list.add(tstatus6);
        list.add(tstatus7);
        return ResultUtil.success(0, "状态", null, list);

    }


    @ApiOperation("提现列表-订单状态-admin-web")
    @GetMapping("getremit_status")
    public ResultBase getremitStatus() {
        Map<String, Object> tstatus1 = new HashMap<>();
        tstatus1.put("keyValue", "SYS_AUDIT");
        tstatus1.put("keyName", "待审核");

        Map<String, Object> tstatus2 = new HashMap<>();
        tstatus2.put("keyValue", "TRADE_RETURN");
        tstatus2.put("keyName", "退回");

        Map<String, Object> tstatus3 = new HashMap<>();
        tstatus3.put("keyValue", "TRADE_SUCCESS");
        tstatus3.put("keyName", "提现成功");

        Map<String, Object> tstatus4 = new HashMap<>();
        tstatus4.put("keyValue", "PART_SUCCESS");
        tstatus4.put("keyName", "部分提现");

        List<Map> list = new ArrayList<>();
        list.add(tstatus1);
        list.add(tstatus2);
        list.add(tstatus3);
        list.add(tstatus4);
        return ResultUtil.success(0, "状态", null, list);
    }

    @ApiOperation("出售列表-订单状态-admin-web")
    @GetMapping("getsell_status")
    public ResultBase getsellStatus() {
        Map<String, Object> tstatus1 = new HashMap<>();
        tstatus1.put("keyValue", "ARE_CANCELLED");
        tstatus1.put("keyName", "挂单中");

        Map<String, Object> tstatus2 = new HashMap<>();
        tstatus2.put("keyValue", "TRADE_SUCCESS");
        tstatus2.put("keyName", "出售完成");

        Map<String, Object> tstatus3 = new HashMap<>();
        tstatus3.put("keyValue", "CANCEL_CANCELLED");
        tstatus3.put("keyName", "取消挂单");
        List<Map> list = new ArrayList<>();
        list.add(tstatus1);
        list.add(tstatus2);
        list.add(tstatus3);
        return ResultUtil.success(0, "状态", null, list);
    }


    @ApiOperation("冻结银行卡")
    @GetMapping(value = "ghostbank")
    @ResponseBody
    public ResultBase freezeGhostBank(HttpServletRequest httpServletRequest) {
        String gbid = httpServletRequest.getParameter("gbid");
        return shopService.freezeGhostBank(gbid);
    }
}
